import { request } from '../utils/request.js'
import store from '../store'
import { Toast } from '../utils/tools.js'
import { baseURL, appType, fileUrl } from '@/common/baseconfig.js'
import cookie from '@/utils/auth.js'
import { isEmpty } from 'libs/fn/verify.js'


//基本配置
export async function GetConfig() {
	try {
		let res = await request({
			url: '/Ctrls/GetConfig',
			method: "post",
			mask: false,
		})
		if (res) {
			let a = {};
			res.forEach(item => {
				a[item.idx] = item.val
			})
			store.commit('general/SET_CONFIG', a)
			return a
		}

	} catch (e) {
		return []
	}
}


//枚举
export async function GetEnum() {
	try {
		let { success, data } = await request({
			url: '/Ctrls/GetEnum',
			method: "post",
			mask: false,
		})
		if (success) {
			store.commit('general/SET_ENUM', data)
		}
	} catch (e) {
		console.log(e)
		return []
	}
}

export async function coinlist() {
	try {
		let res = await request({
			url: '/Ctrls/coinlist',
			method: "post",
			mask: false,
		})
		if (res) {
			let a = {};
			res.forEach(item => {
				a[item.coinid] = item
			})
			store.commit('general/SET_COINCONFIG', a)
		}
	} catch (e) {
		console.log(e)
		return []
	}
}

//上传
export async function Upload(file) {
	try {
		return new Promise((resolve, reject) => {
			uni.showLoading()
			uni.uploadFile({
				url: fileUrl + '/Ctrls/Upload',
				header: {
					'Token': cookie.get('Token', ''),
				},
				filePath: file,
				name: 'upfile',
				success({ data }) {
					if (data) {
						resolve(JSON.parse(data))
					}
				},
				fail: (err) => {
					reject(err);
				},
				complete() {
					uni.hideLoading()
				}
			})
		})
	} catch (e) {
		console.log(e)
		return {}
	}
}

//base64上传
export async function UploadB64(data) {
	try {
		let res = await request({
			url: '/Ctrls/UploadB64',
			method: "post",
			data
		})
		return res
	} catch (e) {
		return []
	}
}

export async function UploadWebUrl(data) {
	try {
		let res = await request({
			url: '/Ctrls/UploadWebUrl',
			method: "post",
			data
		})
		return res
	} catch (e) {
		return []
	}
}


export async function turnAdrr(longitude, latitude,isSet = true) {
	try {
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'GET',
				url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
				data: {
					key: 'ebd6ede86473436a06cc310fb46b5474',
					location: `${longitude},${latitude}`,
					output: 'JSON',
				},
				success: (res) => {

					const { addressComponent, formatted_address } = res.data.regeocode
				
					const {
						country, //国家
						city, //城市
						province, //重庆	
						district, //渝北
						building, //建筑
						neighborhood, //街道
						township, //住宅
						streetNumber //门牌号
					} = addressComponent
					let str1 = isEmpty(streetNumber['street']) ? '' : (streetNumber['street'] +
						streetNumber['number'])
					let str2 = isEmpty(neighborhood['name']) ? '' : neighborhood['name']
					let str3 = isEmpty(building['name']) ? '' : building['name']
					let a = str1 || str2 || str3 || township
					let addressInfo = {
						...addressComponent,
						city: isEmpty(city) ? province : city,
					}
					
					store.commit("address/SET_LOCATION", addressInfo);
					if(isSet){
						store.commit("address/SET_LOCATIONADDRESS", a);
					}

					


					resolve({
						code: true,
						addressComponent: addressInfo,
						address:a,
						formatted_address: formatted_address,
					})
				},
				fail: r => {
					resolve({
						code: false,
						msg: '获取定位失败'
					})
					console.log(errMsg);
				}
			});
		})
	} catch (e) {
		console.log(e);
		return false;
	}
}


export async function wx_token(data) {
	try {
		let res = await request({
			url: '/Users/wx_token',
			method: "post",
			data
		})
		return res
	} catch (e) {
		return []
	}
}

export async function contact(longitude, latitude) {
	try {
		return new Promise(async (resolve, reject) => {

			const token = await wx_token();

			uni.request({
				method: 'POST',
				url: 'https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token=',
				success: (res) => {
					console.log(res)
					resolve({
						addressComponent: res.data.regeocode.addressComponent,
						formatted_address: res.data.regeocode.formatted_address,
					})
				},
				fail: r => {
					resolve(false)
					console.log(r);
				}
			});
		})
	} catch (e) {
		console.log(e);
		return false;
	}
}

//升级
export async function GetAppVersion(paramss) {
	try {
		//默认新旧版为打包时设置的参数   有paramss则覆盖掉
		const params = {
			apptype: appType,
			...paramss
		}
		let res = await request({
			url: '/Ctrls/GetAppVersion',
			method: "post",
			mask: false,
			params
		})
		if (res.success) {
			store.commit('general/SET_UPINFO', res.data[0])
		}
		return res;
	} catch (e) {
		console.log(e)
		return false
	}
}

// 获取验证码
export async function SendCode_Phone(params) {
	try {
		let { success, msg } = await request({
			url: '/Ctrls/SendCode_Phone',
			method: "post",
			params
		})
		Toast(msg)
		return success
	} catch (e) {
		return []
	}
}

export async function SendCode_Mail(params) {
	try {
		let { success, msg } = await request({
			url: '/Ctrls/SendCode_Mail',
			method: "post",
			params
		})
		Toast(msg)
		return success
	} catch (e) {
		return []
	}
}


//app下载地址
export async function GetAllApkVersion() {
	try {
		let { data, success } = await request({
			url: "/Ctrls/GetAllApkVersion",
			method: "post",
		});
		return { data, success };
	} catch (e) {
		return false;
	}
}

export async function gettime() {
	try {
		let res = await request({
			url: "/Ctrls/gettime",
			method: "post",
		});
		return res;
	} catch (e) {
		return false;
	}
}


export async function mlevel() {
	try {
		let res = await request({
			url: "/Ctrls/mlevel",
			method: "post",
		});

		if (res) {
			let a = {};
			res.forEach(item => {
				a[item.userLevel] = item
			})
			return a
		}

	} catch (e) {
		return false;
	}
}


export async function arealist() {
	try {
		let res = await request({
			url: "/Ctrls/arealist",
			method: "post",
		});
		return res

	} catch (e) {
		return false;
	}
}


export async function MsgFeedback_SendMsg(params) {
	try {
		let res = await request({
			url: '/msg/MsgFeedback_SendMsg',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}


export async function GetCardType(params) {
	try {
		let res = await request({
			url: '/Ctrls/GetCardType',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function sysArea(params) {
	try {
		let res = await request({
			url: '/Ctrls/sysArea',
			method: "post",
			params
		})


		store.commit('general/regions', res.data)
		return res
	} catch (e) {
		return false
	}
}



export async function wxPayment(id) {
	try {
		let res = await request({
			url: '/payment/' + id,
			method: "post",
		})
		return res
	} catch (e) {
		return false
	}
}

export async function GetConfigVIP() {
	try {
		let res = await request({
			url: '/Ctrls/GetConfigVIP',
			method: "post",
		})
		return res
	} catch (e) {
		return false
	}
}


export async function Feedback_GetMsg(params) {
	try {
		let res = await request({
			url: '/msg/Feedback_GetMsg',
			method: "post",
			params
		})

		// if (res.success) {
		// 	res.list.forEach(item=>{
		// 		item.content = item.content ? JSON.parse(item.content) : {
		// 			content: "",
		// 			imgList: [],
		// 		}
		// 	})
		// }
		return res
	} catch (e) {
		return false
	}
}